package com.buddies.lottery.ui.lottery.detail;

import android.graphics.Color;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.TypedValue;
import android.view.View;
import android.widget.TextView;

import com.buddies.lottery.R;
import com.buddies.lottery.data.bean.LotteryTabOptions;
import com.buddies.lottery.utils.LotteryUtils;
import com.buddies.lottery.widget.DotView;
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import java.util.ArrayList;
import java.util.List;

public class LotteryTabOptionsAdapter extends BaseMultiItemQuickAdapter<LotteryTabOptions, BaseViewHolder> {

    private final int mLotteryId;
    private List<Integer> mSelectedItems = new ArrayList<>();

    public static final int TAB_HEADER = 0;
    public static final int TAB_OPTION = 1;

    public LotteryTabOptionsAdapter(int lotteryId, List<LotteryTabOptions> tabs) {
        super(tabs);
        mLotteryId = lotteryId;
        addItemType(TAB_HEADER, R.layout.item_lottery_tab_options_header);
        addItemType(TAB_OPTION, R.layout.item_lottery_tab_options);
    }

    @Override
    public void onBindViewHolder(BaseViewHolder holder, int position) {
        super.onBindViewHolder(holder, position);
        if (holder.getItemViewType() == TAB_HEADER) {
            setFullSpan(holder);
        }
    }

    @Override
    protected void convert(BaseViewHolder helper, LotteryTabOptions item) {
        switch (helper.getItemViewType()) {
            case TAB_HEADER:
                helper.setText(R.id.option_name, item.name);
                break;
            case TAB_OPTION:
                helper.itemView.setSelected(mSelectedItems.contains(helper.getLayoutPosition()));
                helper.setText(R.id.option_name, item.name);
                helper.setText(R.id.option_odds, item.odds);

                TextView name = helper.getView(R.id.option_name);
                name.setTextSize(TypedValue.COMPLEX_UNIT_SP, item.name.length() <= 3 || TextUtils.isDigitsOnly(item.name.replace("-", "")) ? 18 : 15);

                DotView tvNums = helper.getView(R.id.tv_nums);
                if (item.nums != null && !item.nums.isEmpty()) {
                    tvNums.setDotNumColors(LotteryUtils.XG6HC_COLOR_NUM_MAP);
                    tvNums.setDots(item.nums);
                    tvNums.setVisibility(View.VISIBLE);
                } else {
                    tvNums.setVisibility(View.GONE);
                }

                if (TextUtils.isDigitsOnly(item.name)) {
                    if (mLotteryId == LotteryUtils.LOTTERY_XG6HC) {
                        name.setBackgroundResource(LotteryUtils.getXG6HCNumColor(item.name));
                        name.setSelected(TextUtils.isDigitsOnly(item.name));
                    } else {
                        name.setSelected(false);
                        name.setBackgroundResource(R.drawable.dot_gray);
                    }
                } else {
                    name.setSelected(false);
                    name.setBackgroundColor(Color.TRANSPARENT);
                }
                break;
        }
    }

    @Override
    public void setOnItemClick(View v, int position) {
        int viewType = getItemViewType(position);
        if (viewType == TAB_OPTION) {
            if (!mSelectedItems.contains(position)) {
                mSelectedItems.add(position);
            } else {
                mSelectedItems.remove((Integer) position);
            }
            notifyDataSetChanged();
            super.setOnItemClick(v, position);
        }
    }

    public List<Integer> getSelectedItems() {
        return mSelectedItems;
    }

    public void clearSelectItems() {
        mSelectedItems.clear();
        notifyDataSetChanged();
    }

    @Override
    public void setNewData(@Nullable List<LotteryTabOptions> data) {
        super.setNewData(data);
        mSelectedItems.clear();
    }

}
